package com.ztgf.order.api.cms.purchase;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.order.models.vo.purchase.PurchaseCommodityLeaveMessageAndReplyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.ztgf.order.models.service.purchase.IPurchaseCommodityLeaveMessageService;
import com.ztgf.order.models.bo.purchase.PurchaseCommodityLeaveMessageSaveBO;
import com.ztgf.order.models.bo.purchase.PurchaseCommodityLeaveMessageBO;
import com.ztgf.order.models.dto.purchase.PurchaseCommodityLeaveMessageDTO;

/**
 * 询价留言表控制器
 *
 * @author mxli
 * @Date 2021-08-24 15:42:41
 */

@RestController
@RequestMapping("/cms/purchaseCommodityLeaveMessage")
@Slf4j
@Api(value = "PurchaseCommodityLeaveMessageController",tags = "询价留言表接口")
public class PurchaseCommodityLeaveMessageController {

    @DubboReference
    private IPurchaseCommodityLeaveMessageService purchaseCommodityLeaveMessageService;

    @ApiOperation(value = "添加询价留言表",notes = "添加询价留言表")
    @PostMapping(value = "/insert")
    public Result insert(PurchaseCommodityLeaveMessageSaveBO saveBO) {
        boolean flag = purchaseCommodityLeaveMessageService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存询价留言表",notes = "保存询价留言表")
    public Result save(PurchaseCommodityLeaveMessageSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改询价留言表",notes = "修改询价留言表")
    @PostMapping("/update")
    public Result update(PurchaseCommodityLeaveMessageSaveBO saveBO){
        PurchaseCommodityLeaveMessageDTO dto = new PurchaseCommodityLeaveMessageDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = purchaseCommodityLeaveMessageService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态",notes = "修改状态")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
             ,@ApiImplicitParam(name = "status",value = "状态：0正常、1屏蔽",required = true,paramType = "query", dataType = "Integer")
     })
     public Result updateStatus(Long id, Integer status){
         PurchaseCommodityLeaveMessageDTO dto = new PurchaseCommodityLeaveMessageDTO();
         dto.setId(id);
         dto.setStatus(status);
        boolean flag = purchaseCommodityLeaveMessageService.updateById(dto);
         if(flag){
             return new Result<>();
         } else {
             return new Result<>(CodeEnum.FAIL_100008);
         }
     }

    @ApiOperation(value = "删除询价留言表",notes = "删除询价留言表")
    @ApiImplicitParam(name = "id",value = "询价留言表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = purchaseCommodityLeaveMessageService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除询价留言表",notes = "批量删除询价留言表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        purchaseCommodityLeaveMessageService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<PurchaseCommodityLeaveMessageDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        PurchaseCommodityLeaveMessageDTO dto = purchaseCommodityLeaveMessageService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "询价留言表列表",notes = "询价留言表列表")
    @PostMapping("/queryPageList")
    public Result<Page<List<PurchaseCommodityLeaveMessageAndReplyVO>>> queryPageList(PurchaseCommodityLeaveMessageBO bo){
        Page<List<PurchaseCommodityLeaveMessageAndReplyVO>> page=  purchaseCommodityLeaveMessageService.queryByPageList(bo);

        return new Result<>(page);
    }
}
